/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('HotelRankListCtrl', ['$scope','$rootScope','$state','$timeout','ionicMaterialMotion', 'ionicMaterialInk','RankService','$filter',
    function($scope,$rootScope, $state,$timeout,ionicMaterialMotion, ionicMaterialInk,RankService,$filter) {
        var hotelId = $scope.$parent.hotelId; //储存在父类中
        /**
         * rankList
         */
        $scope.rankList ={
            page: {
                pageNum:1,
                pageSize:10,
                total:0
            },
            loaded:false,
            data:[],
            loadData:function(okFunc){
                var _this = this,
                    params = {
                        hotelId:hotelId,
                        pageNum:_this.page.pageNum,
                        pageSize:_this.page.pageSize
                    };
                _this.loaded = false;
                RankService.findList(params)
                    .success(function(res){
                        if(res.success){
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            _this.data = _this.data.concat(res.attribute.rows);
                            _this.page.total = res.attribute.total;
                        }else{
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function(error){
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function(){
                    _this.loaded = true;
                    $scope.$broadcast('scroll.refreshComplete');
                });
            },
            refresh:function(){
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        } ;
        $scope.rankDict = {
            'A':'A级',
            'B':'B级',
            'C':'C级'
        };
        /**
         * 编辑
         */
        $rootScope.$on('EDIT_COMPLETE', function (event,result) {
            var rank = result['HotelRankEdit'];
            if(rank){
                $scope.rankList.refresh();
            }
        });
        if(hotelId){
            $scope.rankList.loadData();
        }
}])
.controller('HotelRankDetailCtrl', ['$scope','$rootScope', '$state', 'RankService', '$cordovaToast',
    function($scope,$rootScope, $state, RankService,$cordovaToast) {

        var rankId = $state.params.id,
            rankDict = {
                'A':'A级',
                'B':'B级',
                'C':'C级'
            },
            loadData = function(){
                RankService.findById(rankId)
                    .success(function(res){
                        if(res.success){
                            $scope.rank = res.attribute;
                            $scope.rank.hotelName = $rootScope.EDIT_HOTEL.hotelName;
                            $scope.rank.rankName = rankDict[$scope.rank.rank];
                        }else{
                            $cordovaToast.showShortBottom('获取评级信息失败');
                        }
                    }).error(function(error){
                    console.log(error);
                }).finally(function(){

                });
            };

        $scope.rank = {};

        if(rankId){
            loadData();
        }
    }])
.service('RankService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
    var url = '/crm/hotel-rank/';
    return {
        /**
         * 全部任务
         * @param params
         * @returns {*}
         */
        findList: function (params) {
            var act = 'findHotelRankList';
            return CrmHttp.post(url + act, params);
        },
        /**
         * 获取单个任务
         * @param params
         * @returns {*}
         */
        findById: function (rankId) {
            var act = 'findHotelRankById',
                params = {};
            params['id'] = rankId;
            return CrmHttp.post(url + act, params);
        },
        /**
         * 增加任务
         * @param rank
         * @returns {*}
         */
        insert: function(rank){
            var act = 'insert';
            rank['userId'] = $rootScope.localStorageObj.userId;
            return CrmHttp.post(url + act, rank);

        }
    };
}]);